package com.webinsky.service.impl.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webinsky.common.proj.DelFlag;
import com.webinsky.domain.user.Account;
import com.webinsky.domain.user.AccountFilter;
import com.webinsky.domain.user.Role;
import com.webinsky.persist.dao.user.UserMapper;
import com.webinsky.persist.entity.user.User;
import com.webinsky.service.user.UserManagement;

@Service
public class UserManagementImpl implements UserManagement {
	private static final Logger logger = LoggerFactory.getLogger(UserManagementImpl.class);
	
	@Autowired
	private UserMapper userDao;
	
	private Account convert(User user){
		Account account = new Account();
		account.setId(user.getId());
		account.setLoginName(user.getLogin_name());
		account.setUserName(user.getName());
		account.setPassword(user.getLogin_password());
		account.setUid(String.valueOf(user.getId()));
		account.setSiteId(user.getSiteId());
		Role r = Role.parse(user.getRole());
		account.setRole(r.getRoleId());
		return account;
	}
	
	private User convert(Account account){
		User user = new User();
		user.setLogin_name(account.getLoginName());
		user.setLogin_password(account.getPassword());
		user.setName(account.getUserName());
		user.setRole(account.getRole());
		user.setSiteId(account.getSiteId());
		user.setDel(DelFlag.UNDel.getFlag());
		return user;
	}
	
	@Override
	public Account createAccount(Account user) {
		if ( user != null ){
			User u =  convert(user);
			userDao.insert(u);
			return convert(u);
		}
		return null;
	}

	@Override
	public void updateAccount(Account account) {
		if ( account != null && account.getUid() != null ){
			User u = convert(account);
			u.setId(Integer.valueOf(account.getUid()));
			u.setUpdate_at(new Date());
			u.setDel(DelFlag.UNDel.getFlag());
			userDao.updateByPrimaryKey(u);
		}
	}

	@Override
	public void deleteAccount(int accountId) {
		if ( accountId > 0 ){
			User u = userDao.selectById(accountId);
			if ( u != null && u.getDel() == DelFlag.UNDel.getFlag() ){
				u.setDel(DelFlag.Del.getFlag());
				u.setUpdate_at(new Date());
				userDao.updateByPrimaryKey(u);
			}
		}
	}

	@Override
	public List<Account> filterAccount(AccountFilter filter) {
		List<Account> accounts = new ArrayList<>();
		if ( filter != null ){
			logger.info("[filterAccount] filter account = " + filter);
			List<User> users = userDao.selectByFilter(filter);
			users.forEach(e -> accounts.add(convert(e)));
		}
		return accounts;
	}

	@Override
	public Account getAccountByLoginName(String loginName) {
		List<Account> accounts = new ArrayList<>();
		AccountFilter filter = new AccountFilter();
		filter.setLoginName(loginName);
		if ( filter != null ){
			logger.info("[filterAccount] filter account = " + filter);
			List<User> users = userDao.selectByFilter(filter);
			users.forEach(e -> accounts.add(convert(e)));
		}
		if (accounts.size() == 0) {
			return null;
		}
		return accounts.get(0);
	}

	@Override
	public Account getAccountById(int id) {
		List<Account> accounts = new ArrayList<>();
		AccountFilter filter = new AccountFilter();
		filter.setId(id);
		if ( filter != null ){
			logger.info("[filterAccount] filter account = " + filter);
			List<User> users = userDao.selectByFilter(filter);
			users.forEach(e -> accounts.add(convert(e)));
		}
		if (accounts.size() == 0) {
			return null;
		}
		return accounts.get(0);
	}

}
